package org.linlinjava.litemall.db.service;

import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.linlinjava.litemall.db.dao.LitemallHerbMapper;
import org.linlinjava.litemall.db.domain.LitemallHerb;
import org.linlinjava.litemall.db.domain.LitemallHerbExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class LitemallHerbService {

	@Resource
	private LitemallHerbMapper herbMapper;

	public List<LitemallHerb> querySelective(String code, String herbName, String measurement_unit, String sort,
			String order, Integer page, Integer size, boolean selectAll) {
		LitemallHerbExample example = new LitemallHerbExample();
		LitemallHerbExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(code)) {
			criteria.andCodeLike("%" + code + "%");
		}
		if (!StringUtils.isEmpty(herbName)) {
			criteria.andHerbNameLike("%" + herbName + "%");
		}
		criteria.andDeletedEqualTo(false);
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			example.setOrderByClause(sort + " " + order);
		}
		if (!selectAll) {
			PageHelper.startPage(page, size);
		}

		return herbMapper.selectByExample(example);
	}

	public List<LitemallHerb> selectByGenericName() {
		LitemallHerbExample example = new LitemallHerbExample();
		LitemallHerbExample.Criteria criteria = example.createCriteria();
		criteria.andGenericNameIsNotNull();
		criteria.andDeletedEqualTo(false);
		example.setOrderByClause("generic_name");
		return herbMapper.selectByExample(example);
	}


	public void insertSelective(LitemallHerb herbExample) {
		herbMapper.insertSelective(herbExample);
	}

	public void updateByPrimaryKeySelective(LitemallHerb herbExample) {
		herbMapper.updateByPrimaryKeySelective(herbExample);
	}

	public long countByExample(LitemallHerbExample example) {
		return herbMapper.countByExample(example);
	};

	public List<LitemallHerb> queryHerbByCode(String code) {
		LitemallHerbExample example = new LitemallHerbExample();
		LitemallHerbExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(code)) {
			criteria.andCodeLike("%" + code + "%");
		}
		criteria.andDeletedEqualTo(false);

		return herbMapper.selectByExample(example);
	}

	/**
	 * 获取所有药品品类
	 * 
	 * @return
	 */
	public List<LitemallHerb> getAll(String searchContent) {
		LitemallHerbExample example = new LitemallHerbExample();
		LitemallHerbExample.Criteria criteria = example.createCriteria();

		if (!StringUtils.isEmpty(searchContent)) {
			try {
				String sql = " CONCAT(IFNULL(herb_name,''),IFNULL(code,''),IFNULL(specifications,'')) LIKE '%"
						+ searchContent + "%' ";
				Class<?> criteriaCls = LitemallHerbExample.Criteria.class;
				Class<?> generatedCriteriaCls = criteriaCls.getSuperclass();
				Method addCriterion = generatedCriteriaCls.getDeclaredMethod("addCriterion", String.class);
				addCriterion.setAccessible(true);
				addCriterion.invoke(criteria, sql);
			} catch (Exception e) {

			}
		}

		criteria.andDeletedEqualTo(false);
		return herbMapper.selectByExample(example);
	}

	/**
	 * 获取所有药品品类
	 * 
	 * @return
	 */
//	public Map<String, Object> getAllHerbVoList(String searchContent, Integer page, Integer limit) {
//		PageHelper.startPage(page, limit);
//		List<LitemallHerb> herbList = getAll(searchContent);
//		List<Map<String, Object>> herbVoList = new ArrayList<>(herbList.size());
//		Map<String, Object> vo = null;
//		for (LitemallHerb obj : herbList) {
//			vo = new HashMap<>();
//
//			vo.put("id", obj.getId());
//			vo.put("code", obj.getCode());
//			vo.put("name", obj.getHerbName());
//			vo.put("specifications", obj.getSpecifications());
//			vo.put("unit", obj.getMeasurementUnit());
//			vo.put("content", MessageFormat.format("{0} - {1}", obj.getHerbName(), formatStr(obj.getSpecifications())));
//			vo.put("contentAll", MessageFormat.format("{0} - {1}/{2}", obj.getHerbName(),
//					formatStr(obj.getSpecifications()), formatStr(obj.getMeasurementUnit())));
//
//			herbVoList.add(vo);
//		}
//		Page<Map<String, Object>> list = (Page) herbList;
//		
//		Map<String, Object> data = new HashMap();
//		data.put("list", herbVoList);
//		data.put("total", list.getTotal());
//		data.put("page", list.getPageNum());
//		data.put("limit", list.getPageSize());
//		data.put("pages", list.getPages());
//
//		return data;
//	}

	private String formatStr(String value) {
		return StringUtils.isEmpty(value) ? "" : value;
	}

	public List<LitemallHerb> selectByExample(LitemallHerbExample example) {
		return herbMapper.selectByExample(example);
	}

	public int batchInsert(List<LitemallHerb> list) {
		return herbMapper.batchInsert(list);
	}


    /**
     * 获取所有药材总数，不包括已删除的药材
     *
     * @return
     */
    public int count() {
    	LitemallHerbExample example = new LitemallHerbExample();
        example.or().andDeletedEqualTo(false);
        return (int) herbMapper.countByExample(example);
    }

}
